﻿CREATE PROCEDURE [dbo].[ArticleSales_UpdateFromTempPIDifferences]
AS
	UPDATE ArticleSales	
	SET PIDiffUnits = temp.PIDiffUnits,
		PIDiff = temp.PIDiff,
		ChargeOffsUnits = temp.ChargeOffsUnits,
		ChargeOffs = temp.ChargeOffs,
		Rebates = temp.Rebates
	FROM ArticleSales s 
	Inner Join ( Select ArticleId, StoreId, Date, AVG(PIDiffUnits) PIDiffUnits, AVG(PIDiff) PIDiff, AVG(ChargeOffsUnits) ChargeOffsUnits, AVG(ChargeOffs) ChargeOffs, AVG(Rebates) Rebates
				 From TempPIDifferences
				 Group By ArticleId, StoreId, Date) temp on s.Date = temp.Date and s.ArticleId = temp.ArticleId and s.StoreId = temp.StoreId